﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using TekaMUD.Engine.Framework;
using TekaMUD.Engine.ServerTasks;
using TekaMUD.Engine;

namespace TekaMUD.Engine.Workers
{
	/// <summary>
	/// Thread to run server tasks like:
	///		- Weather updates.
	///		- Sending messages to the room.
	///		- Updating agents.
	///		- Expiring objects.
	///		- Updating resources.
	/// </summary>
	[Obsolete("Is this worker really needed ?")]
	public class WorkerThreadServerTasks : WorkerThread
	{
		public WorkerThreadServerTasks() : base("Server Tasks Thread")
		{
		}

		protected override void Run()
		{
			while (!Terminated)
			{
				if(Terminated)
					goto cleanup;

				// The order of the following 2 statements is relevant.
				Task st = ServerTasksQueue.DequeueTask();
				Locks.StorageLock.EnterUpgradeableReadLock();

				Logger.LogDebug("Executing Task: " + st.ToString());
				st.Execute();

				Locks.StorageLock.ExitUpgradeableReadLock();
				Thread.Sleep(Period);
			}

		cleanup:
			Logger.LogInfo("[Thread] Server Tasks terminated.");
		}
	}
}
